package cz.data.domain.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.system.model.dto.MenuDto;
import cz.data.domain.system.model.entity.MenuEntity;
import cz.data.domain.system.model.vo.MenuVo;
import cz.data.domain.system.mapstruct.MenuMapper;
import cz.data.domain.system.service.MenuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"系统管理-菜单"})
@RestController
@RequestMapping("/menus")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "系统管理-菜单-详情", notes = "根据url的id来获取资源详细信息")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<MenuVo> getMenuById(@PathVariable String id) {
        MenuEntity menuEntity = menuService.getById(id);
        return new SimpleDataResponse<>(menuMapper.toVO(menuEntity));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "系统管理-菜单-列表", notes = "")
    @GetMapping("/list")
    public SimpleDataResponse<List<MenuVo>> getMenuList() {
        QueryWrapper<MenuEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(MenuEntity::getCreateTime);
        List<MenuEntity> list = menuService.list(wrapper);
        List<MenuVo> collect = list.stream().map(menuMapper::toVO).collect(Collectors.toList());
        return new SimpleDataResponse<>(collect);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "系统管理-菜单-新增", notes = "根据menu对象创建资源")
    @ApiImplicitParam(name = "menu", value = "资源详细实体menu", required = true, dataType = "MenuDto")
    @PostMapping()
    public SimpleDataResponse<MenuVo> saveMenu(@RequestBody @Validated({ValidationGroups.Insert.class}) MenuDto menu) {
        MenuEntity menuEntity = menuService.saveMenu(menu);
        return new SimpleDataResponse<>(menuMapper.toVO(menuEntity));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "系统管理-菜单-更新", notes = "根据url的id来指定更新对象，并根据传过来的menu信息来更新资源详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "menu", value = "资源详细实体menu", required = true, dataType = "MenuDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<MenuVo> updateMenu(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) MenuDto menu) {
        MenuEntity menuEntity = menuService.updateMenu(menu);
        return new SimpleDataResponse<>(menuMapper.toVO(menuEntity));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "系统管理-菜单-删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "资源ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteMenu(@PathVariable String id) {
        menuService.deleteMenuById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "系统管理-菜单树-节点删除", notes = "递归删除节点下所有菜单")
    @ApiImplicitParam(name = "id", value = "节点ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/tree/{id}")
    public BaseResponse deleteMenuByNodeId(@PathVariable String id) {
        menuService.deleteMenuByTreeId(id);
        return BaseResponse.success();
    }
}

